package com.ghy.springbootrabbitmq.production;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class ReturnCallbackService implements RabbitTemplate.ReturnCallback {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init(){
        rabbitTemplate.setReturnCallback(this);             //指定 ReturnCallback
    }

    /**
     *
     * @param message 消息对象
     * @param i 错误码
     * @param s 错误信息
     * @param s1 交换机
     * @param s2 路由键
     */
    @Override
    public void returnedMessage(Message message ,int i ,String s ,String s1 ,String s2) {
        System.out.println("消息对象:" + message);
        System.out.println("错误码:" + i);
        System.out.println("错误信息:" + s);
        System.out.println("消息使用的交换器:" + s1);
        System.out.println("消息使用的路由key:" + s2);
        //业务代码处理
    }
}
